/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package troqler;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.sql.DataSource;

/**
 *
 * @author haga
 */
public class Oversikt implements Serializable {

    private String bruker;
    private ArrayList<TreningsOkt> alleOkter = new ArrayList<TreningsOkt>();
    private int nesteNr = 1;
    private DataSource ds;
    private ResultSet res = null;
    private PreparedStatement ps = null;
    private Connection con = null;

    public Oversikt(Connection con, String bruker) throws SQLException {

        try {

            ps = con.prepareStatement("SELECT oktnr, dato, varighet, kategorinavn, tekst FROM"
                    + " trening where brukernavn=?");
            this.bruker = bruker;
            ps.setString(1, bruker);
            if (ps.execute()) {
                res = ps.getResultSet(); 
                while (res.next()) {
                    TreningsOkt okt = new TreningsOkt(res.getInt("oktnr"), res.getDate("dato"),
                            res.getInt("varighet"), res.getString("kategorinavn"), res.getString("tekst"));
                    alleOkter.add(okt);
                }
            }
        } catch (SQLException e) {
            throw e;
        } finally {
            ryddOppDB();
        }


    }

    public String getBruker() {
        return bruker;
    }

    public void setBruker(String bruker) {
        this.bruker = bruker;
    }
    
    

    private void ryddOppDB() throws SQLException {
        if (res != null) {
            res.close();
            res = null;
        }
        if (ps != null) {
            ps.close();
            ps = null;
        }

    }

    public List<TreningsOkt> getAlleOkter() {
        return alleOkter;
    }

    public void registrerNyOkt(TreningsOkt okt, Connection con) throws SQLException {
        this.con = con;
        try {
            ps = con.prepareStatement("INSERT INTO trening"
                    + "(dato, varighet, kategorinavn, tekst, brukernavn)"
                    + " VALUES(?, ?, ?, ?, ?)",Statement.RETURN_GENERATED_KEYS);

            ps.setDate(1, new java.sql.Date(okt.getDato().getTime()));
            ps.setInt(2, okt.getVarighet());
            ps.setString(3, okt.getKategori());
            ps.setString(4, okt.getTekst());
            ps.setString(5, bruker);
            ps.execute();
            res = ps.getGeneratedKeys();
            if (res.next()) {
                okt.setNr(res.getInt(1));
            }

            alleOkter.add(okt);
        } catch (SQLException e) {
        } finally {
            ryddOppDB();
        }


    }

    public int getSum() {
        int sum = 0;
        for (TreningsOkt o : alleOkter) {
            sum += o.getVarighet();
        }
        return sum;
    }

    public int getNesteNr() {
        return ++nesteNr;
    }

    public int getAntall() {
        return alleOkter.size();
    }

    public void slettOkt(TreningsOkt okt, Connection con) throws SQLException {
        this.con = con;
        try {
            ps = con.prepareStatement("DELETE FROM trening WHERE oktnr = ?");
            ps.setInt(1, okt.getNr());
            if (ps.executeUpdate() > 0) {
                alleOkter.remove(okt);
            }
        } catch (SQLException e) {
        } finally {
            ryddOppDB();
        }
    }

    public void oppdaterOkt(TreningsOkt okt, Connection con) throws SQLException {
        this.con = con;
        try {
            ps = con.prepareStatement("UPDATE trening"
                    + " SET dato=?, varighet=?, kategorinavn=?, tekst=?"
                    + " WHERE oktnr=?");
            ps.setDate(1, new java.sql.Date(okt.getDato().getTime()));
            ps.setInt(2, okt.getVarighet());
            ps.setString(3, okt.getKategori());
            ps.setString(4, okt.getTekst());
            ps.setInt(5, okt.getNr());
            ps.executeUpdate();

        } catch (SQLException e) {
        } finally {
            ryddOppDB();
        }
    }

    public List<TreningsOkt> getOkterForMnd(Integer aar, Integer maaned) {
        Calendar cal1 = Calendar.getInstance();
        int mnd = (maaned >= 0 ? maaned : 1);
        if (aar != null) {
            cal1.set(aar, mnd, 1);
        } else {
            cal1.set(2000, mnd, 1);
        }
        Calendar cal2 = Calendar.getInstance();
        int m2 = cal1.get(Calendar.MONTH);
        ArrayList<TreningsOkt> nyListe = new ArrayList<TreningsOkt>();
        for (TreningsOkt o : alleOkter) {

            cal2.setTime(o.getDato());
            if (aar != null && maaned >= 0 && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)
                    && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)) {
                nyListe.add(o);
                continue;
            } else if (aar != null && maaned < 0 && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR)) {
                nyListe.add(o);
                continue;
            } else if (maaned >= 0 && aar == null
                    && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)) {
                nyListe.add(o);
                continue;
            }

        }
        return nyListe;
    }
}
